package com.example.ujsfish.activity;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.widget.TextView;

import com.example.ujsfish.R;
import com.example.ujsfish.utils.AppNetService;
import com.example.ujsfish.model.Coordinate;
import com.example.ujsfish.model.monitor.Data;
import com.example.ujsfish.model.monitor.JsonRootBean;
import com.example.ujsfish.utils.DeviceName;
import com.example.ujsfish.utils.RetrofitManager;

import java.util.Timer;
import java.util.TimerTask;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

import static java.lang.Thread.sleep;

public class MonitorActivity extends AppCompatActivity {

    private Coordinate coordinate = Coordinate.getInstance();

    private TextView text_CurrentLon;
    private TextView text_CurrentLat;
    private TextView text_CurrentBattery;
    private TextView text_CurrentFeed;
    private TextView text_paofu;
    private TextView text_CurrentSpeed;
    private TextView text_XiaLiao;
    private TextView text_X;
    private TextView text_Y;

    String id = DeviceName.getDeviceInstance().getDeviceId().get(0);


    RetrofitManager myRetrofitManager = RetrofitManager.getInstance();
    Retrofit myRetrofit = RetrofitManager.getRetrofit();
    AppNetService appNetService = myRetrofitManager.getApiService(AppNetService.class);
    Timer timer;
    TimerTask task;

    private String TAG = "test";

    private void getMonitorData(AppNetService appNetService) {
        Call<JsonRootBean> call1 = appNetService.postMonitor(id);
        call1.enqueue(new Callback<JsonRootBean>() {
            @Override
            public void onResponse(Call<JsonRootBean> call, Response<JsonRootBean> response) {
                System.out.println(response.toString());
                try{
                    JsonRootBean jsonRootBean = response.body();
                    Data monitorData = jsonRootBean.getData().get(0);
                    updateUI(monitorData);
                }catch (Exception e){
                    System.out.println(e);
                }

            }

            @Override
            public void onFailure(Call<JsonRootBean> call, Throwable t) {
                Log.d(TAG, "onFailure: " + t.toString());
            }
        });
    }

    private void updateUI(Data monitorData) {
        text_CurrentLon.setText(monitorData.getCorrectLongitude());
        text_CurrentLat.setText(monitorData.getCorrectLatitude());
        text_CurrentBattery.setText(monitorData.getQuantity());
        text_X.setText(monitorData.getX_coordinate());
        text_Y.setText(monitorData.getY_coordinate());
        text_CurrentFeed.setText(monitorData.getClout());
        //text_paofu.setText(String.valueOf();下行数据中好像没有
        text_CurrentSpeed.setText(monitorData.getSpeed());

        float v = Float.parseFloat(monitorData.getCheck_code()) / 100;

        text_XiaLiao.setText(String.valueOf(v));
        Log.d(TAG, "xialiao------------>" + monitorData.getCheck_code());
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.monitor_data_activity);

        //设置菜单栏返回按钮
        ActionBar actionBar = this.getSupportActionBar();
        actionBar.setTitle("监测数据");
        actionBar.setDisplayHomeAsUpEnabled(true);

        initView();

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == android.R.id.home)
        {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }



    private void initView() {
        text_CurrentLon = findViewById(R.id.text_CurrentLon);
        text_CurrentLat = findViewById(R.id.text_CurrentLat);
        text_CurrentBattery= findViewById(R.id.text_CurrentBattery);
        text_CurrentFeed= findViewById(R.id.text_CurrentFeed);
        text_paofu= findViewById(R.id.text_paofu);
        text_CurrentSpeed= findViewById(R.id.text_CurrentSpeed);
        text_XiaLiao = findViewById(R.id.text_XiaoLiao);
        text_X = findViewById(R.id.text_CurrentX);
        text_Y = findViewById(R.id.text_CurrentY);
    }

    @Override
    protected void onResume() {
        super.onResume();
        super.onResume();
        timer = new Timer();
        task = new TimerTask() {
            @Override
            public void run() {
                getMonitorData(appNetService);
            }
        };
        timer.schedule(task,0,2000);
        System.out.println("定时器开启");

    }

    @Override
    protected void onPause() {
        super.onPause();
        timer.cancel();
        System.out.println("定时器关闭");
    }

}